| 1 |  |  | /* global it describe before */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 | 1 |  | process.env.NODE_ENV = 'test'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | //Require the dev-dependencies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 | 1 |  | const chai = require('chai'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 | 1 |  | const chaiHttp = require('chai-http'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 | 1 |  | const HTMLParser = require('node-html-parser'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 1 |  | const server = require('../../app.js'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 1 |  | chai.should(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 1 |  | const db = require("../db/database.js"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 1 |  | const { exec } = require('child_process'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 1 |  | chai.use(chaiHttp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 1 |  | let apiKey = ""; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 22 | 1 |  | describe('copier', () => { | 
            
                                                        
            
                                    
            
            
                | 23 | 1 |  |     before(() => { | 
            
                                                        
            
                                    
            
            
                | 24 | 1 |  |         return new Promise((resolve) => { | 
            
                                                        
            
                                    
            
            
                | 25 | 1 |  |             db.run("DELETE FROM products", (err) => { | 
            
                                                        
            
                                    
            
            
                | 26 | 2 |  |                 if (err) { | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |                     console.error("Could not empty test DB table orders", err.message); | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 30 | 1 |  |                 db.run("DELETE FROM orders", (err) => { | 
            
                                                        
            
                                    
            
            
                | 31 | 2 |  |                     if (err) { | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |                         console.error("Could not empty test DB table orders", err.message); | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |                     } | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 35 | 1 |  |                     db.run("DELETE FROM order_items", (err) => { | 
            
                                                        
            
                                    
            
            
                | 36 | 2 |  |                         if (err) { | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |                             console.error("Could not empty test DB table orders", err.message); | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |                         } | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 40 | 1 |  |                         exec( | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |                             'cat v2/db/seed.sql | sqlite3 v2/db/test.sqlite', | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |                             (error, stdout, stderr) => { | 
            
                                                        
            
                                    
            
            
                | 43 | 2 |  |                                 if (error) { | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |                                     console.error(error.message); | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |                                     return; | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |                                 } | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 48 | 2 |  |                                 if (stderr) { | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |                                     console.error(stderr); | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |                                     return; | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |                                 } | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 53 | 1 |  |                                 resolve(); | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |                             }); | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |                     }); | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |                 }); | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |             }); | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |     }); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 61 | 1 |  |     describe('POST /copier/products', () => { | 
            
                                                        
            
                                    
            
            
                | 62 | 1 |  |         it('should get 401 as we do not provide valid api_key', (done) => { | 
            
                                                        
            
                                    
            
            
                | 63 | 1 |  |             chai.request(server) | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |                 .post("/v2/copier/products") | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |                 .end((err, res) => { | 
            
                                                        
            
                                    
            
            
                | 66 | 1 |  |                     res.should.have.status(401); | 
            
                                                        
            
                                    
            
            
                | 67 | 1 |  |                     res.body.should.be.an("object"); | 
            
                                                        
            
                                    
            
            
                | 68 | 1 |  |                     res.body.errors.status.should.be.equal(401); | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 | 1 |  |                     done(); | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |                 }); | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 74 | 1 |  |         it('should get 200 as we get apiKey', (done) => { | 
            
                                                        
            
                                    
            
            
                | 75 | 1 |  |             let user = { | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |                 email: "[email protected]", | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |                 gdpr: "gdpr" | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |             }; | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 80 | 1 |  |             chai.request(server) | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |                 .post("/v2/auth/api_key/confirmation") | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |                 .send(user) | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |                 .end((err, res) => { | 
            
                                                        
            
                                    
            
            
                | 84 | 1 |  |                     res.should.have.status(200); | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 86 | 1 |  |                     let HTMLResponse = HTMLParser.parse(res.text); | 
            
                                                        
            
                                    
            
            
                | 87 | 1 |  |                     let apiKeyElement = HTMLResponse.querySelector('#apikey'); | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 89 | 1 |  |                     apiKey = apiKeyElement.childNodes[0].rawText; | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 | 1 |  |                     done(); | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |                 }); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 95 | 1 |  |         it('should get 201 HAPPY PATH, 10 products should have been created', (done) => { | 
            
                                                        
            
                                    
            
            
                | 96 | 1 |  |             chai.request(server) | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |                 .post("/v2/copier/products") | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |                 .send({ api_key: apiKey }) | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |                 .end((err, res) => { | 
            
                                                        
            
                                    
            
            
                | 100 | 1 |  |                     res.should.have.status(201); | 
            
                                                        
            
                                    
            
            
                | 101 | 1 |  |                     res.body.should.be.an("object"); | 
            
                                                        
            
                                    
            
            
                | 102 | 1 |  |                     res.body.data.should.be.an("array"); | 
            
                                                        
            
                                    
            
            
                | 103 | 1 |  |                     res.body.data.length.should.equal(10); | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 | 1 |  |                     done(); | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |                 }); | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 109 | 1 |  |         it('should get 201 HAPPY PATH, only 10 in response', (done) => { | 
            
                                                        
            
                                    
            
            
                | 110 | 1 |  |             chai.request(server) | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |                 .post("/v2/copier/products") | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |                 .send({ api_key: apiKey }) | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |                 .end((err, res) => { | 
            
                                                        
            
                                    
            
            
                | 114 | 1 |  |                     res.should.have.status(201); | 
            
                                                        
            
                                    
            
            
                | 115 | 1 |  |                     res.body.should.be.an("object"); | 
            
                                                        
            
                                    
            
            
                | 116 | 1 |  |                     res.body.data.should.be.an("array"); | 
            
                                                        
            
                                    
            
            
                | 117 | 1 |  |                     res.body.data.length.should.equal(10); | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 119 | 1 |  |                     done(); | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |                 }); | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |     }); | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 124 | 1 |  |     describe("POST /copy_all", () => { | 
            
                                                        
            
                                    
            
            
                | 125 | 1 |  |         it('should get 401 as we do not provide valid api_key', (done) => { | 
            
                                                        
            
                                    
            
            
                | 126 | 1 |  |             chai.request(server) | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |                 .post("/v2/copier/all") | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |                 .end((err, res) => { | 
            
                                                        
            
                                    
            
            
                | 129 | 1 |  |                     res.should.have.status(401); | 
            
                                                        
            
                                    
            
            
                | 130 | 1 |  |                     res.body.should.be.an("object"); | 
            
                                                        
            
                                    
            
            
                | 131 | 1 |  |                     res.body.errors.status.should.be.equal(401); | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 133 | 1 |  |                     done(); | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |                 }); | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 137 | 1 |  |         it('should get 200 as we get apiKey', (done) => { | 
            
                                                        
            
                                    
            
            
                | 138 | 1 |  |             let user = { | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |                 email: "[email protected]", | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |                 gdpr: "gdpr" | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |             }; | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 143 | 1 |  |             chai.request(server) | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |                 .post("/v2/auth/api_key/confirmation") | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |                 .send(user) | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |                 .end((err, res) => { | 
            
                                                        
            
                                    
            
            
                | 147 | 1 |  |                     res.should.have.status(200); | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 149 | 1 |  |                     let HTMLResponse = HTMLParser.parse(res.text); | 
            
                                                        
            
                                    
            
            
                | 150 | 1 |  |                     let apiKeyElement = HTMLResponse.querySelector('#apikey'); | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 152 | 1 |  |                     apiKey = apiKeyElement.childNodes[0].rawText; | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 154 | 1 |  |                     done(); | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |                 }); | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 158 | 1 |  |         it('should get 201 HAPPY PATH', (done) => { | 
            
                                                        
            
                                    
            
            
                | 159 | 1 |  |             chai.request(server) | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |                 .post("/v2/copier/all") | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |                 .send({ api_key: apiKey }) | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |                 .end((err, res) => { | 
            
                                                        
            
                                    
            
            
                | 163 | 1 |  |                     res.should.have.status(201); | 
            
                                                        
            
                                    
            
            
                | 164 | 1 |  |                     res.body.should.be.an("object"); | 
            
                                                        
            
                                    
            
            
                | 165 | 1 |  |                     res.body.data.should.have.property("products"); | 
            
                                                        
            
                                    
            
            
                | 166 | 1 |  |                     res.body.data.should.have.property("orders"); | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 168 | 1 |  |                     res.body.data.products.should.be.an("array"); | 
            
                                                        
            
                                    
            
            
                | 169 | 1 |  |                     res.body.data.products.length.should.equal(10); | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 171 | 1 |  |                     res.body.data.orders.should.be.an("array"); | 
            
                                                        
            
                                    
            
            
                | 172 | 1 |  |                     res.body.data.orders.length.should.equal(4); | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 174 | 1 |  |                     res.body.data.orders[0].order_items.should.be.an("array"); | 
            
                                                        
            
                                    
            
            
                | 175 | 1 |  |                     res.body.data.orders[0].order_items.length.should.equal(2); | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 177 | 1 |  |                     done(); | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |                 }); | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 180 |  |  |     }); | 
            
                                                        
            
                                    
            
            
                | 181 |  |  | }); | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |  |